package com.ruoyi.resume.utils.deepseek;

import com.ruoyi.resume.utils.deepseek.dto.ChatRequest;
import com.ruoyi.resume.utils.deepseek.dto.ChatResponse;
import com.ruoyi.resume.utils.deepseek.dto.Message;
import com.ruoyi.resume.utils.deepseek.dto.Options;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.List;

@Service
public class DeepSeekClient {

    private final RestTemplate restTemplate;
    private final String API_URL = "http://59.77.5.2:12345/api/chat";  // 根据实际API路径修改

    public DeepSeekClient(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public ChatResponse getAnswer(String prompt) {
        // 创建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("x-some-header", "some-value");

        // 构建请求体
        ChatRequest request = new ChatRequest();
        request.setModel("deepseek-r1:32b");

        Message message = new Message();
        message.setRole("user");
        message.setContent(prompt);
        request.setMessages(Arrays.asList(message));

        Options options = new Options();
        options.setNumCtx(8192);
        request.setOptions(options);

        // 创建HTTP实体
        HttpEntity<ChatRequest> entity = new HttpEntity<>(request, headers);

        // 发送请求[响应体的格式不对应]
        ResponseEntity<ChatResponse> response = restTemplate.exchange(
                API_URL,
                HttpMethod.POST,
                entity,
                ChatResponse.class
        );

        return response.getBody();
    }
}